// Constants
static final int COST_BARRICADE = 0;

// Object instances

Player player;
ActorManager am;
PImage nextWaveButton;
PImage continueButton;

// Global variables
boolean horizontal = false;
public int state = 0;
int money = 100;
int difficulty = 1;
int currentWave = 0;
boolean spawn = true;
public boolean waveFinished = false;
public boolean waveStarted = false;

void setup()
{
 size(displayWidth - 100, displayHeight - 100);
 
 // Initiate object instance
 am = new ActorManager();
 player = new Player(width/2, height - 64);
 nextWaveButton = loadImage("nxtwavephone.png");
 continueButton = loadImage("continuephone.png");
 
}

void draw()
{
  background(0);
  textSize(30);
  fill(0, 255, 0);
  text("$"+ money, width/2 - 10, 50);
  
  // If in build phase
  if(state == 0)
  {
      waveStarted = false;
      spawn = true;
      text("Build Phase", 50, 50);
    
      //Display next wave button
      tint(255);
      image(nextWaveButton, 10, displayHeight - 200);
      
      //Display rotate button
   
      // Render marker for barricade placement
      stroke(50);
      fill(255, 0);
      if(!horizontal)
      {
      //Vertical
      rect(mouseX, mouseY, 32, 64);
      }
      else
      {
      //Horizontal
      rect(mouseX, mouseY, 64, 32);
      }
         
  }
  // If in combat phase
  else if(state == 1)
  {
    text("Combat Phase", 50, 50);
    
    //display continue button after wave
    if(waveFinished)
    {
      tint(255);
      image(continueButton, 10, displayHeight - 200);
    }
    
    if(spawn)
    {
      for(int i = 0; i <= difficulty; ++i)
      {
      am.addEnemy(0);
      }
      ++currentWave;
      waveStarted = true;
    spawn = false;
    ++difficulty;
    }
    
    
    
    player.Render();
    player.Update();
  }
  
  // Handle
  am.Render();
  am.Update();
}                     

void mousePressed(){

    
    if(money > 0 && state == 0)
    {
      if(mouseX > 69 && mouseY < displayHeight - 200)
      {
        am.addPlaceable(horizontal, mouseX, mouseY);
        money -= COST_BARRICADE;
      }
    }
    
    //Input combat
    if(state == 1)
    {
       am.addProjectile(mouseX+29, height-64); 
       
       // If continue button is pressed
       if(mouseX < 69 && mouseX > 14 &&
       mouseY < displayHeight - 200 + 50 && mouseY > displayHeight - 200)
       {
         if(mousePressed && waveFinished)
         {
           state = 0;
         }      
       }
    }
    //Input build
    else if(state == 0)
    {
      // If next wave button pressed
      if(mouseX < 69 && mouseX > 14 &&
         mouseY < displayHeight - 200 + 50 && mouseY > displayHeight - 200)
         {
           if(mousePressed)
           {
             state = 1;
           }      
         }
         
       
    }  
}

//Placeholder to drag barricades instead of just clicking

// as long as you click and drag, it will give the box a new position
void mouseDragged()
{

}

// if you let go of a mouse button, it's not locked to mouse position
void mouseReleased()
{
  
}


void keyPressed()
  {
    // Barricade rotation toogle
    if (key == 'r' || key == 'R')
    {
      if(horizontal)
      {
        horizontal = false;
      }
      else if(!horizontal)
      {
        horizontal = true;
      }
    }
    //Toogle phase
    else if( key == 't' || key == 'T')
    {
       if(state == 0)
        state = 1;
       else if(state == 1)
        state = 0; 
    }
  }


